package com.wd.db;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 除了线程池之外的线程使用，如,main未初始化线程池，需要数据库操作
 * 
 * @author long
 * @Create 2012-10-19 下午3:24:16
 * @QQ 108232706
 * @version 1.0
 * @description CommonDaoManager
 */
public class CommonDaoManager {
	private static DBMgr[] dbMgrs;
	private final static Map<Class<?>, BaseDao[]> daos = new HashMap<Class<?>, BaseDao[]>();
	private static int threadCount;
	private static boolean isInit = false;

	public static synchronized boolean init(int _threadCount, final Class<?>[] iclzs, final Class<? extends BaseDao>[] clzs) throws Throwable {
		if (isInit)
			return true;
		threadCount = _threadCount;
		int count = 0;
		dbMgrs = new DBMgr[threadCount];
		do {
			dbMgrs[count] = new DBMgr(count);
			count++;
		} while (count < threadCount);
		isInit = initDao(iclzs, clzs);
		return isInit;
	}

	public static synchronized boolean initDao(final Class<?>[] iclzs, final Class<? extends BaseDao>[] clzs) {
		if (clzs.length != iclzs.length)
			throw new IllegalArgumentException("clzs implements iclzs must has same lenth");
		for (int i = 0; i < iclzs.length; i++) {
			if (!iclzs[i].isAssignableFrom(clzs[i])) {
				throw new IllegalArgumentException("clzs must implements iclzs ,at index:" + i);
			}
		}
		int count = 0;
		boolean suc = true;
		Class<? extends BaseDao> clz;
		for (int i = 0; i < clzs.length; i++) {
			if (daos.get(iclzs[i]) != null)
				continue;
			clz = clzs[i];
			BaseDao[] arrDao = new BaseDao[threadCount];
			suc = true;
			try {
				try {
					Constructor c1 = clz.getDeclaredConstructor(DBMgr.class);
					try {
						for (int j = 0; j < arrDao.length; j++) {
							arrDao[j] = (BaseDao) c1.newInstance(dbMgrs[j]);
						}
						count++;

					} catch (IllegalArgumentException e) {
						suc = false;
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						suc = false;
						e.printStackTrace();
					}
				} catch (SecurityException e) {
					suc = false;
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					suc = false;
					e.printStackTrace();
				}

			} catch (InstantiationException e) {
				suc = false;
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				suc = false;
				e.printStackTrace();
			}
			if (suc)
				daos.put(iclzs[i], arrDao);
		}
		return count == clzs.length;

	}

	public synchronized static boolean close() throws SQLException {

		for (int i = 0; i < threadCount; i++) {
			dbMgrs[i].close();
		}
		return true;

	}

	public synchronized static <T> T DAO(Class<T> iclz) {
		return DAO(iclz, 0);
	}

	public synchronized static <T> T DAO(Class<T> iclz, int index) {
		if (index < 0 || index >= threadCount)
			throw new IllegalArgumentException("index invalid:" + index);
		return (T) daos.get(iclz)[index];
	}

}
